<?php


namespace App\Models\Admin;


class AdminOperateLogs extends Base
{
    protected $table = 'admin_operate_logs';
    protected $guarded = [];
    protected $dateFormat = 'U';


    public function module()
    {
        return $this->hasOne(AdminOperateModule::class,'id','module')->select('id','name');
    }

    public function operate()
    {
        return $this->hasOne(AdminOperateModule::class,'id','operate')->select('id','name');
    }

    public function type()
    {
        return $this->hasOne(AdminOperateType::class,'id','type')->select('id','name');
    }

    public function admin()
    {
        return $this->hasOne(AdminUser::class,'id','user_id')->select('id','name');
    }

    public static function getListByMainId($id,$model)
    {
        $list=self::where('main_id',$id)
            ->where('model',$model)
            ->with([
                'admin:id,name',
                'operate:id,name',
            ])
            ->orderBy('id','desc')
            ->select('id','user_id','ip','operate','created_at','content')
            ->get()->transform(function ($i)
            {
                $i->admin_name=$i->admin->name;
                unset($i->admin);
                return $i;
            });
        return $list;
    }
}
